<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Config;

class AdminController extends Controller {

    protected $_response;

    public function __construct()
    {
        $this->_response                            = array();
        $this->_response['_title']                  = '小一农货';
        $this->_response['_active']                 = array();
        $this->_response['_active']['_model']       = '';
        $this->_response['_active']['_action']      = '';
    }

    public function mapMark($arr){
        $default = array(
            "_id"            => null,
            "store_id"       => null,
            "_name"          => null,
            "_location"      => null,
            "_address"       => null,
        );

        $param = array(
            "key" 		=> Config::get('amap.key'),
            "tableid"	=> Config::get('amap.tableid'),
            "loctype"	=> "1",
        );

        $data = array_merge($default,$arr);
        $amapAddress = array(
            "_name"		=> $data['_name'],
            "_location" => $data['_location'],
            "_address"	=> $data['_address'],
            "store_id"	=> $data['store_id']
        );


        if(!empty($data['_id'])){
            //update
            $amapAddress['_id'] = $data['_id'];
            $url = Config::get('amap.update_url');
        }else{
            //create
            $url = Config::get('amap.url');
        }
        $param['data'] = json_encode($amapAddress);

        $amap = json_decode(curlPost($url , $param));

        if(!empty($amap) && $amap->status == '1'){

            $sign['is_sign'] = 1;
            $sign['amap_id'] = isset($amap->_id) ? $amap->_id : $data['_id'] ;

            return $sign['amap_id'];
        }else{
            return false;
        }

    }

    
    /**
     * 获取分页的HTML内容
     * @param integer $page 当前页
     * @param integer $pages 总页数
     * @param string $url 跳转url地址    最后的页数以 '&page=x' 追加在url后面
     *
     * @return string HTML内容;
     */
    public static function getPageHtml($page, $pages, $url){
        //最多显示多少个页码
        $_pageNum = 5;
        //当前页面小于1 则为1
        $page = $page<1?1:$page;
        //当前页大于总页数 则为总页数
        $page = $page > $pages ? $pages : $page;
        //页数小当前页 则为当前页
        $pages = $pages < $page ? $page : $pages;

        //计算开始页
        $_start = $page - floor($_pageNum/2);
        $_start = $_start<1 ? 1 : $_start;
        //计算结束页
        $_end = $page + floor($_pageNum/2);
        $_end = $_end>$pages? $pages : $_end;

        //当前显示的页码个数不够最大页码数，在进行左右调整
        $_curPageNum = $_end-$_start+1;
        //左调整
        if($_curPageNum<$_pageNum && $_start>1){
            $_start = $_start - ($_pageNum-$_curPageNum);
            $_start = $_start<1 ? 1 : $_start;
            $_curPageNum = $_end-$_start+1;
        }
        //右边调整
        if($_curPageNum<$_pageNum && $_end<$pages){
            $_end = $_end + ($_pageNum-$_curPageNum);
            $_end = $_end>$pages? $pages : $_end;
        }

        $_pageHtml = '<ul class="pagination">';
        if($_start != 1){
            $_pageHtml .= '<li><a  title="第一页" href="'.$url.'&page=1">首页</a></li>';
        }
        if($page>1){
            $_pageHtml .= '<li><a  title="上一页" href="'.$url.'&page='.($page-1).'">上一页</a></li>';
        }
        for ($i = $_start; $i <= $_end; $i++) {
            if($i == $page){
                $_pageHtml .= '<li class="active"><a>'.$i.'</a></li>';
            }else{
                $_pageHtml .= '<li><a href="'.$url.'&page='.$i.'">'.$i.'</a></li>';
            }
        }
        if($page<$_end){
            $_pageHtml .= '<li><a  title="下一页" href="'.$url.'&page='.($page+1).'">下一页</a></li>';
        }

        if($_end != $pages){
            $_pageHtml .= '<li><a  title="最后一页" href="'.$url.'&page='.$pages.'">尾页</a></li>';
        }

        $_pageHtml .= '</ul>';
        return $_pageHtml;
    }
}